package org.googlecode.lightning.search.parse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.daragon.fulltext.parse.parser.Parser;
import org.daragon.fulltext.segment.Holder;

/**
 * Parses by delegating to some other parser (looked up by MIME type),
 * 
 * @author daragon
 *
 */
public class DelegateParser implements Parser {

    private static final Log LOG = LogFactory.getLog(DelegateParser.class);
    
    private ParserFactory parserFactory;

    public DelegateParser(ParserFactory parserFactory) {
        this.parserFactory = parserFactory;
    }
    
    public String parse(String uri, Holder holder) {
        String text = null;
        Parser parser = parserFactory.getParser(holder.getMimeType());
        if (parser == null) {
            LOG.info("no parser for " + uri + ", content type " + holder.getMimeType() );
        } else {
            try {
            	text = parser.parse(uri, holder);
                if (text == null) {
                    LOG.debug("parse of " + uri + " failed with first parser");
                }
            } catch (Throwable t) {
                LOG.debug("uncaught exception during parse of " + uri , t);
            }
        }
        return text;
    }

}
